package com.example.testapplication.ui.home;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.ColorStateList;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.DecelerateInterpolator;

import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;

import com.example.testapplication.BR;
import com.example.testapplication.R;
import com.example.testapplication.adapter.FragmentPagerAdapter;
import com.example.testapplication.databinding.FragHomeBinding;
import com.example.testapplication.event.Event;
import com.example.testapplication.ui.home.home_list_fragment.HomeListFragment;
import com.example.testapplication.ui.home.home_web_fragment.BrowserFragment;
import com.example.testapplication.widget.XCollapsingToolbarLayout;
import com.gyf.immersionbar.ImmersionBar;

import net.lucode.hackware.magicindicator.ViewPagerHelper;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.ColorTransitionPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.SimplePagerTitleView;

import me.goldze.mvvmhabit.base.BaseFragment;
import me.goldze.mvvmhabit.bus.RxBus;
import me.jessyan.autosize.utils.AutoSizeUtils;

public class HomeFragment extends BaseFragment<FragHomeBinding,HomeViewModel> implements XCollapsingToolbarLayout.OnScrimsListener {

    private FragmentPagerAdapter mPagerAdapter;
    private String[] titles = new String[]{"新闻列表","网页展示"};

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.frag_home;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }


    @Override
    public void initData() {
        // 给这个 ToolBar 设置顶部内边距，才能和 TitleBar 进行对齐
//        ImmersionBar.setTitleBar(getActivity(), binding.ctlHomeBar);

        initFragments();
        initIndicator();
        binding.ctlHomeBar.setOnScrimsListener(this);
    }


    private void initIndicator() {
        CommonNavigator commonNavigator = new CommonNavigator(getActivity());
        //设置铺满
        commonNavigator.setAdjustMode(true);
        commonNavigator.setAdapter(new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return titles.length;
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {
                SimplePagerTitleView simplePagerTitleView = new ColorTransitionPagerTitleView(context);
                simplePagerTitleView.setText(titles[index]);
                simplePagerTitleView.setNormalColor(getResources().getColor(R.color.gray));
                simplePagerTitleView.setSelectedColor(getResources().getColor(R.color.teal_200));
                simplePagerTitleView.setTextSize(TypedValue.COMPLEX_UNIT_DIP,16);

//                simplePagerTitleView.getPaint().setFakeBoldText(true);
                simplePagerTitleView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        binding.vpHomePager.setCurrentItem(index);
                    }
                });
                return simplePagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                LinePagerIndicator indicator = new LinePagerIndicator(context);
                indicator.setColors(getResources().getColor(R.color.teal_200));
                indicator.setMode(LinePagerIndicator.MODE_EXACTLY);
                indicator.setStartInterpolator(new AccelerateInterpolator());
                indicator.setEndInterpolator(new DecelerateInterpolator(1.6f));
                indicator.setLineHeight(AutoSizeUtils.dp2px(context, 2));
                indicator.setLineWidth(AutoSizeUtils.dp2px(context, 20));
                return indicator;
            }

            @Override
            public float getTitleWeight(Context context, int index) {
                return 2.0f;
            }
        });
        binding.magicIndicator.setNavigator(commonNavigator);
        ViewPagerHelper.bind(binding.magicIndicator, binding.vpHomePager);
    }

    @SuppressLint("RestrictedApi")
    @Override
    public void onScrimsStateChange(XCollapsingToolbarLayout layout, boolean shown) {
        ImmersionBar.with(this)
                // 指定导航栏背景颜色
                .navigationBarColor(R.color.transparent)
                // 状态栏字体和导航栏内容自动变色，必须指定状态栏颜色和导航栏颜色才可以自动变色
                .autoDarkModeEnable(true, 0.2f)
                .statusBarDarkFont(shown).init();
        //通知activity改变状态栏颜色
        RxBus.getDefault().post(new Event(shown));
        binding.tvHomeAddress.setTextColor(ContextCompat.getColor(getActivity(), shown ? R.color.black : R.color.white));
        binding.tvHomeHint.setBackgroundResource(shown ? R.drawable.home_search_bar_gray_bg : R.drawable.home_search_bar_transparent_bg);
        binding.tvHomeHint.setTextColor(ContextCompat.getColor(getActivity(), shown ? R.color.black60 : R.color.white60));
        binding.ivHomeSearch.setSupportImageTintList(ColorStateList.valueOf(getColor(shown ? R.color.common_icon_color : R.color.white)));
    }

    private void initFragments() {
        mPagerAdapter = new FragmentPagerAdapter<>(this);
        mPagerAdapter.addFragment(new HomeListFragment());
        mPagerAdapter.addFragment(BrowserFragment.newInstance("https://www.baidu.com/"));
        binding.vpHomePager.setAdapter(mPagerAdapter);

    }
}
